package mobileann.mafamily.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import mobileann.mafamily.entity.SecurityAppEntity;

import com.mofind.android.base.L;
import com.mofind.android.net.HttpUtils;
import com.mofind.android.net.JSONParser;
import com.mofind.android.net.HttpUtils.HttpResultCallback;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

/**
 * 主要功能:判断是否安装有主流的杀毒软件
 * @annotation ADRIAN.20151117 
 *
 */
public class ListSettingUtils {
	private static ListSettingUtils _instance;
	public static final String PKG_360SAFE="com.qihoo360.mobilesafe";//360卫士
	public static final String PKG_BAIDUSAFE="cn.opda.a.phonoalbumshoushou";//百度卫士
	public static final String PKG_KINGSOFTSAFE="com.ijinshan.mguard";//金山手机卫士
	public static final String PKG_360JIKE="com.qihoo.antivirus";//360极客版
	public static final String PKG_360CLEAR="com.qihoo.cleandroid_cn";//360清理大师
	public static final String PKG_TENCENT="com.tencent.qqpimsecure";//腾讯手机管家
	public static final String PKG_LBESAFE="com.lbe.security";// LBE安全大师
	public static final String PKG_SAFEMAN="com.anguanjia.safe";//安全管家
	public static final String PKG_LIEBAOCLEAR="com.cleanmaster.mguard_cn";//猎豹清理大师
	public static final String PKG_OPPOELEC="com.coloros.oppoguardelf";//oppo电量节省"
	public static final String PKG_OPPOBACKGROUND="com.oppo.purebackground";//纯净后台 

	/**
	 * 单例模式返回类
	 * @return
	 */
	public synchronized static ListSettingUtils getInstance() {
		if (_instance == null) {
			_instance = new ListSettingUtils();
		}
		return _instance;
	}
	
	/**
	 * 获取到到所有的杀毒软件
	 * @param c
	 * @return
	 */
	public List<SecurityAppEntity> getAllApk(Context c){
		PackageManager pm=c.getPackageManager();
		// 查询所有已经安装的应用程序  
		List<ApplicationInfo> listAppcations = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
		Collections.sort(listAppcations, new ApplicationInfo.DisplayNameComparator(pm));// 排序 , 便于查找
		List<SecurityAppEntity> pkgs=new ArrayList<SecurityAppEntity>();
		for(int i=0, count = listAppcations.size(); i < count; i++){
			ApplicationInfo applicationInfo=listAppcations.get(i);
			SecurityAppEntity securityApp=new SecurityAppEntity();
			String pkgName=applicationInfo.packageName;
			String name=AppDetailsManager.getInstance().getAppName(c, pkgName);
			if(pkgName.equals(PKG_360SAFE)){
				//		pkgNames.add(pkgName);
				if(name==null){
					name="360卫士";
				}
				securityApp.setAppName(name);
				securityApp.setPkName(pkgName);
				pkgs.add(securityApp);
				continue;
			}
			if(pkgName.equals(PKG_360JIKE)){
				//		pkgNames.add(pkgName);
				if(name==null){
					name="360极客版";
				}
				securityApp.setAppName(name);
				securityApp.setPkName(pkgName);
				pkgs.add(securityApp);
				continue;
			}
			if(pkgName.equals(PKG_360CLEAR)){
				//		pkgNames.add(pkgName);
				if(name==null){
					name="360清理大师";
				}
				securityApp.setAppName(name);
				securityApp.setPkName(pkgName);
				pkgs.add(securityApp);
				continue;
			}
			if(pkgName.equals(PKG_BAIDUSAFE)){
				//		pkgNames.add(pkgName);
				if(name==null){
					name="百度卫士";
				}
				securityApp.setAppName(name);
				securityApp.setPkName(pkgName);
				pkgs.add(securityApp);
				continue;
			}
			if(pkgName.equals(PKG_KINGSOFTSAFE)){
				//		pkgNames.add(pkgName);
				if(name==null){
					name="金山手机卫士";
				}
				securityApp.setAppName(name);
				securityApp.setPkName(pkgName);
				pkgs.add(securityApp);
				continue;
			}
			if(pkgName.equals(PKG_TENCENT)){
				//		pkgNames.add(pkgName);
				if(name==null){
					name="腾讯手机管家";
				}
				securityApp.setAppName(name);
				securityApp.setPkName(pkgName);
				pkgs.add(securityApp);
				continue;
			}
			if(pkgName.equals(PKG_LBESAFE)){
				//		pkgNames.add(pkgName);
				if(name==null){
					name="LBE安全大师";
				}
				securityApp.setAppName(name);
				securityApp.setPkName(pkgName);
				pkgs.add(securityApp);
				continue;
			}
			if(pkgName.equals(PKG_SAFEMAN)){
				//		pkgNames.add(pkgName);
				if(name==null){
					name="安全管家";
				}
				securityApp.setAppName(name);
				securityApp.setPkName(pkgName);
				pkgs.add(securityApp);
				continue;
			}
			if(pkgName.equals(PKG_LIEBAOCLEAR)){
				//		pkgNames.add(pkgName);
				if(name==null){
					name="猎豹清理大师";
				}
				securityApp.setAppName(name);
				securityApp.setPkName(pkgName);
				pkgs.add(securityApp);
				continue;
			}
			if(pkgName.equals(PKG_OPPOELEC)){
				//		pkgNames.add(pkgName);
				if(name==null){
					name="oppo电量节省";
				}
				securityApp.setAppName(name);
				securityApp.setPkName(pkgName);
				pkgs.add(securityApp);
				continue;
			}
			if(pkgName.equals(PKG_OPPOBACKGROUND)){
				//		pkgNames.add(pkgName);
				if(name==null){
					name="纯净后台";
				}
				securityApp.setAppName(name);
				securityApp.setPkName(pkgName);
				pkgs.add(securityApp);
				continue;
			}
		}
		if(PhoneInfoUtils.getPhoneModel().startsWith("HUAWEI")){
			SecurityAppEntity securityApp=new SecurityAppEntity();
			securityApp.setAppName("华为手机");
			securityApp.setPkName("com.huawei.emui");
			pkgs.add(securityApp);
		}
		return pkgs;
	}
	
	/**
	 * 
	 * @param c
	 * @return true-有杀毒软件, false-没有杀毒软件
	 */
	public boolean isShowSafeApp(Context c){
		boolean isShowSafeApp=false;
		if(getAllApk(c).size()==0){
			isShowSafeApp=false;
		}else{
			isShowSafeApp=true;
		}
		return isShowSafeApp;
	}

	/**
	 * 
	 * @param uid
	 * @param urad
	 * @param securityApps
	 * @return
	 */
	private static Map<String, String> getSecurityPara(String uid, String urad,
			List<SecurityAppEntity> securityApps) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("from", "add_security");
		map.put("uid", uid);
		map.put("data", TripleDESUtil.CBCEncode(uid, urad,securityApps.toString()));
		L.i("secur","LLL"+securityApps.toString());
		return map;
	}

	/**
	 * 将所使用的App发送到服务器, 估计是要统计什么的 @ADRIAN.20151117
	 * @param uid
	 * @param urad
	 * @param securityApps
	 */
	public void doSecurityApp(final String uid, final String urad, final List<SecurityAppEntity> securityApps) {
		HttpUtils.startHttpPost(URLUtils.URL_GROWTH_UPLOAD, getSecurityPara(uid, urad, securityApps),
				new HttpResultCallback() {
			@Override
			public void result(String url, boolean isResult,
					String jsonStr) {
				if (isResult) {
					String json = TripleDESUtil.getJsonObjStr(uid, urad, jsonStr);
					JSONObject jo = JSONParser.getJSONObject(json);
					String status = JSONParser.getString(jo, "status");
					L.i("ListSettingUtils#doSecurityApp#"+json);
				}
			}
		});
	}
}
